﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spring.Core;
using Spring.Objects.Factory.Config;
using Spring.Objects.Factory.Support;

namespace Strongshell.Recoil.Core.Integration
{
    public class ObjectDefinitionRegistryConfigurer : IObjectFactoryPostProcessor, IOrdered
    {
        private readonly IList<string> wiringContainerTypeNames;

        public ObjectDefinitionRegistryConfigurer(IList<string> wiringContainerTypeNames)
        {
            this.wiringContainerTypeNames = wiringContainerTypeNames;
        }

        public void PostProcessObjectFactory(IConfigurableListableObjectFactory factory)
        {
            var registry = factory as IObjectDefinitionRegistry;

            if (registry == null)
            {
                throw new ArgumentException(
                    "The object factory must implement IObjectDefinitionRegistry so that object definitions may be inserted.");
            }

            foreach (var containerTypeName in wiringContainerTypeNames)
            {
                Type containerType = Type.GetType(containerTypeName);

                registry.Configure().With(containerType);
            }
        }

        public int Order
        {
            get { return 0; }
        }
    }
}
